package net.tngou.iask.action.api;

import org.apache.commons.lang3.math.NumberUtils;

import net.tngou.iask.action.BaseAction;
import net.tngou.iask.enums.LevelEnum;
import net.tngou.iask.pojo.Analysis;
import net.tngou.iask.pojo.Analysis_patient;
import net.tngou.iask.service.AnalysisService;
import net.tngou.iask.util.StringUtil;

/**
 * 自定义分析
 * @author tngou@tngou.net
 *
 */
public class AnalysisApi extends BaseAction {

	
	/**
	 * 添加自定义分析
	 */
	public void add() {
		if(user==null){
			run_nologin();return;
		}
		Analysis bean = getAsk(Analysis.class);
		if(isEmpty(bean.getName(),bean.getDescription()))
		{
			run_false("请求的参数不正确！");return;
		}
		bean.setUser(user.getId());
		long id = bean.save();
		if(id>0){
			saveLog(LevelEnum.system, "添加自定义统计分析(id:"+id+"-"+bean.getName()+")");
			run_success(id+"");return;
		}
		else {run_false("添加失败！");return;}
	}
	
	/**
	 * 编辑自定义分析
	 */
	public void update() {
		if(user==null){
			run_nologin();return;
		}
		Analysis bean = getAsk(Analysis.class);
		if(bean.getId()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Analysis analysis = new Analysis().get(bean.getId());
		if(analysis==null||analysis.getUser()!=user.getId())
		{
			run_false("请求的数据不正确！");return;
		}
		if(StringUtil.isNotEmpty(bean.getName()))analysis.setName(bean.getName());
		if(StringUtil.isNotEmpty(bean.getDescription()))analysis.setDescription(bean.getDescription());
		long id = analysis.update();
		if(id>0){
			saveLog(LevelEnum.system, "更新自定义统计分析(id:"+id+"-"+analysis.getName()+")");
			run_success("更新成功");return;
		}
		else {run_false("更新失败！");return;}
	}
	
	/**
	 * 删除自定义分析
	 */
	public void delete() {
		if(user==null){
			run_nologin();return;
		}
		if(ask.getId()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Analysis analysis = new Analysis().get(ask.getId());
		if(analysis==null||analysis.getUser()!=user.getId())
		{
			run_false("请求的数据不正确！");return;
		}
		AnalysisService service = new AnalysisService();
		boolean b = service.delete(analysis);
		if(b){
			saveLog(LevelEnum.system, "删除自定义统计分析(id:"+ask.getId()+"-"+analysis.getName()+")");
			run_success("删除成功");return;
		}
		else {run_false("删除失败！");return;}
				
	}
	
	
	/**
	 * 添加分析条目
	 */
	public void questionadd() {
		if(user==null){
			run_nologin();return;
		}
		if(user==null){
			run_nologin();return;
		}
		String question=request.getParameter("question");
		if(ask.getId()==0||isEmpty(question)||!NumberUtils.isNumber(question))
		{
			run_false("请求的参数不正确！");return;
		}
		Analysis analysis = new Analysis().get(ask.getId());
		if(analysis==null||analysis.getUser()!=user.getId())
		{
			run_false("请求的数据不正确！");return;
		}
		AnalysisService service = new AnalysisService();
	    boolean b = service.saveQuestion(analysis.getId(), Long.parseLong(question));
	    if(b){
			saveLog(LevelEnum.system, "添加自定义统计分析参数(id:"+ask.getId()+"-"+analysis.getName()+")");
			analysis.setQuestioncount(analysis.getQuestioncount()+1);
			analysis.update();
			run_success("添加成功");return;
		}
		else {run_false("添加失败！");return;}
	}
	
	/**
	 * 删除统计参数
	 */
	public void questiondelete() {
		if(user==null){
			run_nologin();return;
		}
		if(user==null){
			run_nologin();return;
		}
		String question=request.getParameter("question");
		if(ask.getId()==0||isEmpty(question)||!NumberUtils.isNumber(question))
		{
			run_false("请求的参数不正确！");return;
		}
		Analysis analysis = new Analysis().get(ask.getId());
		if(analysis==null||analysis.getUser()!=user.getId())
		{
			run_false("请求的数据不正确！");return;
		}
		AnalysisService service = new AnalysisService();
	    boolean b = service.deleteQuestion(analysis.getId(), Long.parseLong(question));
	    if(b){
			saveLog(LevelEnum.system, "添加自定义统计分析参数(id:"+ask.getId()+"-"+analysis.getName()+")");
			analysis.setQuestioncount(analysis.getQuestioncount()-1);
			analysis.update();
			run_success("添加成功");return;
		}
		else {run_false("添加失败！");return;}
	}
	
	/**
	 * 患者入组
	 */
	public void patientadd() {
		if(user==null){
			run_nologin();return;
		}
		String patient=request.getParameter("patient");
		if(ask.getId()==0||isEmpty(patient)||!NumberUtils.isNumber(patient))
		{
			run_false("请求的参数不正确！");return;
		}
		Analysis analysis = new Analysis().get(ask.getId());
		if(analysis==null||analysis.getUser()!=user.getId())
		{
			run_false("请求的数据不正确！");return;
		}
		AnalysisService service = new AnalysisService();
	    boolean b = service.savePatient(analysis.getId(), Long.parseLong(patient));
	    if(b){
			saveLog(LevelEnum.system, "添加自定义统计分析参数(id:"+ask.getId()+"-"+analysis.getName()+")");
			analysis.setPatientcount(analysis.getPatientcount()+1);
			analysis.update();
			run_success("添加成功");return;
		}
		else {run_false("添加失败！");return;}
	}
	
	/**
	 * 病人更新
	 */
	public void patientupdate() {
		if(user==null){
			run_nologin();return;
		}	
		if(ask.getId()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Analysis_patient bean = getAsk(Analysis_patient.class);
		Analysis_patient analysis_p= new Analysis_patient().get(ask.getId());
		if(analysis_p==null)
		{
			run_false("请求的数据不正确！");return;
		}
		if(bean.getBdate()!=null)analysis_p.setBdate(bean.getBdate());
		long id = analysis_p.update();
		if(id>0){
				saveLog(LevelEnum.system, "更新自定义统计分析患者(id:"+ask.getId());
				run_success("修改成功");return;
		}else {run_false("修改失败！");return;}
		
	}
	
	/**
	 * 删除患者
	 */
	public void patientdelete() {
		if(user==null){
			run_nologin();return;
		}
		String patient=request.getParameter("patient");
		if(ask.getId()==0||isEmpty(patient)||!NumberUtils.isNumber(patient))
		{
			run_false("请求的参数不正确！");return;
		}
		Analysis analysis = new Analysis().get(ask.getId());
		if(analysis==null||analysis.getUser()!=user.getId())
		{
			run_false("请求的数据不正确！");return;
		}
		AnalysisService service = new AnalysisService();
	    boolean b = service.deletePatient(analysis.getId(), Long.parseLong(patient));
	    if(b){
			saveLog(LevelEnum.system, "添加自定义统计分析参数(id:"+ask.getId()+"-"+analysis.getName()+")");
			analysis.setPatientcount(analysis.getPatientcount()-1);
			analysis.update();
			run_success("添加成功");return;
		}
		else {run_false("添加失败！");return;}
	}
	
}
